﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class PurviewService
    {
        /// <summary>
        /// 查询权限资源信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Purview GetPurview(long id)
        {
            Purview purview = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    purview = new Purview();
                    purview.Id = id;
                    purview = new PurviewDAO().GetPurview(conn, purview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return purview;
        }

        /// <summary>
        /// 获取权限资源列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable GetPurviewTable(int pageindex, int pagesize)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Purview purview = new Purview();
                    purview.Pageindex = pageindex;
                    purview.Pagesize = pagesize;
                    dt = new PurviewDAO().GetPurviewTable(conn, purview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 获取权限资源记录数
        /// </summary>
        /// <returns></returns>
        public long GetPurviewRecord()
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Purview purview = new Purview();
                    record = new PurviewDAO().GetPurviewRecord(conn, purview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 获取权限资源记录数
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public long GetPurviewRecord(String code)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Purview purview = new Purview();
                    purview.Code = code;
                    record = new PurviewDAO().GetPurviewRecord(conn, purview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入权限资源信息
        /// </summary>
        /// <param name="purview"></param>
        /// <returns></returns>
        public long InsertPurview(Purview purview)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new PurviewDAO().InsertRecord(conn, purview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新权限资源信息
        /// </summary>
        /// <param name="purview"></param>
        /// <returns></returns>
        public int UpdatePurview(Purview purview)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new PurviewDAO().UpdatePurview(conn, purview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除权限资源信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeletePurview(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        PurviewDAO purviewDAO = new PurviewDAO();
                        Purview purview = new Purview();
                        foreach (String lopid in arr)
                        {
                            purview.Id = Int64.Parse(lopid);
                            count += purviewDAO.DeletePurview(conn, purview);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
